//    Copyright (C) Kherty.  All rights reserved.
namespace OpenLS.Spreadsheet.Expressions
{
    class ConcatenateExpression: BinaryExpressionBase<Expression>
    {
        public ConcatenateExpression(Expression left, Expression right):
            base (left, right)
        {

        }
        public override string GetText(TextContext context)
        {
            return Left.GetText(context) + "&" + Right.GetText(context);
        }
        protected override Expression CreateExpression(Expression left, Expression right)
        {
            return new ConcatenateExpression(left, right);
        }
        internal override Expression Transposed(Cell target)
        {
            return new ConcatenateExpression(Left.Transposed(target), Right.Transposed(target));
        }


        protected override object GetObjectImp(EvaluationContext context)
        {
            object l = Left.GetValue(context);
            object r = Right.GetValue(context);
            if (l is Error)
                return l;
            if (r is Error)
                return r;
            return ObjectToString(l) + ObjectToString(r);
        }
        static string ObjectToString(object o)
        {
            if (o == null)
                return "";
            return o.ToString();
        }

    }
}
